package com.company.clientscard.main.widgets.general;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public abstract class SelectItemDialogBox<T> extends DialogBox {

	VerticalPanel dialogContents = new VerticalPanel();
	HorizontalPanel buttonPanel = new HorizontalPanel();
	SimplePager pager = new SimplePager();

	T selectedItem;
	private boolean selected = false;

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public SelectItemDialogBox(final TableDataProvider<T> dataProvider, T object) {

		setGlassEnabled(true);
		setAnimationEnabled(true);
		// setModal(true);

		setText("Select item");
		setWidget(dialogContents);

		// Create a CellTable.
		CellTable<T> table = new CellTable<T>(dataProvider.getKeyProvider());
		TextColumn<T> nameColumn = new TextColumn<T>() {

			@Override
			public String getValue(T object) {
				// TODO Auto-generated method stub
				return getName(object);
			}
		};
		// Add a selection model to handle user selection.
		final SingleSelectionModel<T> selectionModel = new SingleSelectionModel<T>(
				dataProvider.getKeyProvider());
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

					@Override
					public void onSelectionChange(SelectionChangeEvent event) {
						// TODO Auto-generated method stub
						T selected = selectionModel.getSelectedObject();
						if (selected != null) {
							setSelectedItem(selected);

							if (5 != 0) {

								pager.setPage(dataProvider.getRanges()[0]
										.getStart() / 5);
							}
						}
					}
				});

		table.setSelectionModel(selectionModel);

		table.addColumn(nameColumn, "Name");
		dataProvider.addDataDisplay(table);
		table.setPageSize(5);

		pager.setPageSize(5);
		pager.setDisplay(table);

		if (object != null) {
			selectionModel.setSelected(object, true);
		}

		// Add the columns.

		Button closeButton = new Button("Close", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				setSelected(false);
				hide();
			}
		});

		Button selectButton = new Button("Select", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if (getSelectedItem() != null) {
					setSelected(true);
					hide();

				} else {
					Window.alert("You didn't selected any item, press close button");
				}
			}
		});

		Button selectNullButton = new Button("Set NULL", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				setSelected(true);
				setSelectedItem(null);
				hide();
			}
		});

		buttonPanel.add(selectButton);
		buttonPanel.setCellHorizontalAlignment(selectButton,
				HasAlignment.ALIGN_LEFT);
		buttonPanel.add(selectNullButton);
		buttonPanel.setCellHorizontalAlignment(selectNullButton,
				HasAlignment.ALIGN_LEFT);

		buttonPanel.add(closeButton);
		buttonPanel.setCellHorizontalAlignment(closeButton,
				HasAlignment.ALIGN_RIGHT);

		dialogContents.add(table);
		dialogContents.add(pager);
		dialogContents.setCellHorizontalAlignment(pager,
				HasHorizontalAlignment.ALIGN_CENTER);
		dialogContents.add(buttonPanel);
	}

	public abstract String getName(T object);

	public T getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(T selectedItem) {
		this.selectedItem = selectedItem;
	}

}
